package com.tianfei.crowd.service.Impl;

import com.tianfei.crowd.entity.Auth;
import com.tianfei.crowd.entity.AuthExample;
import com.tianfei.crowd.mapper.AuthMapper;
import com.tianfei.crowd.service.api.AuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author: Herz
 * @date: 2021/7/18 11:00
 */
@Service
public class AuthServiceImpl implements AuthService {

    @Autowired
    AuthMapper authMapper;


    @Override
    public List<Auth> getAllAuth() {

        return authMapper.selectByExample(new AuthExample());
    }

    @Override
    public List<Integer> getAssignedAuthIdByRoleId(Integer roleId) {
        return authMapper.selectAssignedAuthIdByRoleId(roleId);
    }

    @Override
    public void saveAssignAuthToRole(Map<String, List<Integer>> map) {

        // 1、获取当前的roleId
        List<Integer> roleIdList = map.get("roleId");
        Integer roleId = roleIdList.get(0);

        // 2、根据当前的roleId 删除旧的 角色-权限关联关系
        authMapper.deleteOldRelationship(roleId);

        // 3、根据当前的roleId 保存新的 角色-权限关联关系
        List<Integer> authIdArray = map.get("authIdArray");

        // 4、判断  authIdArray 是否有效
        if (authIdArray != null && authIdArray.size() !=0) {
            authMapper.insertNewRelationship(roleId,authIdArray);
        }

    }

    @Override
    public List<String> getAssignedAuthNameByAdminId(Integer adminId) {
        return authMapper.selectAssignedAuthNameByAdminId(adminId);
    }
}
